﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Easydp.Models.Entities;
using Easydp.Models;
using System.Data;
namespace Easydp.Controllers
{
    [DataObject]
    public class NewsController
    {
        NewsManager manager = new NewsManager();
        [DataObjectMethod(DataObjectMethodType.Select)]
        public DataSet GetDataByCategoryOrArea(string category,string area)
        {
            if (string.IsNullOrEmpty(category))
                category = "0";
            if (string.IsNullOrEmpty(area))
                area = "0";
            try
            {
                return manager.GetDataset(Convert.ToInt32(area), Convert.ToInt32(category));
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        [DataObjectMethod(DataObjectMethodType.Select)]
        public DataSet GetDataTopByCategoryOrArea(string category, string area)
        {
            if (string.IsNullOrEmpty(category))
                category = "0";
            if (string.IsNullOrEmpty(area))
                area = "0";
            try
            {

                DataSet ds = manager.GetDataset(Convert.ToInt32(area), Convert.ToInt32(category));
                DataView dv = ds.Tables[0].DefaultView;
                dv.Sort = " hits desc,NewsDate desc,CreateOn desc";
                return ds;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }


        /// <summary>
        /// 类别排行TOP
        /// </summary>
        /// <returns></returns>
        [DataObjectMethod(DataObjectMethodType.Select)]
        public DataSet GetIndexTypeTop()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@"select  DISTINCT top 10 a.categoryid, MAX(a.hits) as hits,a.newsid,b.text as categoryText,a.Title,a.NewsDate
                        from TT_News as a
                        left join NewsCategory as b on a.CategoryID = b.NewsCategoryID
                        group by a.categoryid,a.newsid,b.text,a.Title,a.NewsDate
                        order by NewsDate desc,hits desc");
            try
            {
                return manager.GetDataset(sql.ToString());
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 热帖排行10
        /// </summary>
        /// <returns></returns>
        [DataObjectMethod(DataObjectMethodType.Select)]
        public DataSet GetIndexTop10()
        {
             StringBuilder sql = new StringBuilder();
             sql.Append(@"select  DISTINCT top 10  MAX(hits) as hits,newsid,Title,NewsDate
                            from TT_News 
                          -- Where CreateOn BETWEEN (getdate()-5) and getdate()
                            group by newsid,title,NewsDate
                            order by NewsDate desc,hits desc");
            try
            {
                return manager.GetDataset(sql.ToString());
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 最新资讯
        /// </summary>
        /// <returns></returns>
        [DataObjectMethod(DataObjectMethodType.Select)]
        public DataSet GetIndexNew4(int rows)
        {
            
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.AppendFormat(@"select top {0} newsid,title from TT_News order by createon desc",(rows<=0)?4:rows);
                return manager.GetDataset(sql.ToString());
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 目的地新20
        /// </summary>
        /// <returns></returns>
        [DataObjectMethod(DataObjectMethodType.Select)]
        public DataSet GetIndexRegionTop20()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@"select  DISTINCT top 20 a.AreaID, a.hits,a.newsid,b.text as areaText,a.Title,a.newsdate
                         from TT_News as a
                         left join Area as b on a.AreaID = b.ID
                         order by a.newsdate desc,hits desc");
            try
            {
                return manager.GetDataset(sql.ToString());
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 头条新闻
        /// </summary>
        /// <returns></returns>
        [DataObjectMethod(DataObjectMethodType.Select)]
        public DataSet GetIndexBigNews(int rows)
        {
            
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.AppendFormat(@"select top 1 TopNewsID,NewsID,Title,Summary from TT_TopNews order BY CreateOn desc", (rows <= 0) ? 1 : rows);
                return manager.GetDataset(sql.ToString());
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        

        [DataObjectMethod(DataObjectMethodType.Select)]
        public DataSet SelectSingle(string key)
        {
            try
            {
                return manager.GetDataset(Convert.ToInt32(key));
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
    }
}
